package com.crawler.crawler.util;
import javax.net.ssl.*;
import java.io.*;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.Random;
import java.util.UUID;


public class FileDownload {

    //链接url下载图片
    public static String downloadPicture(String fileUrl,String filePath) {
        URL url = null;
        int imageNumber = 0;
        int last=fileUrl.lastIndexOf(".");
        String suffix="";
        if(last>-1){
            suffix=fileUrl.substring(last);
        }
        String saveFileName= (UUID.randomUUID().toString().replaceAll("-",""))+suffix;
        try {

            //覆盖我的项目中的SSL检查
            /* Start of Fix */
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; }
                public void checkClientTrusted(X509Certificate[] certs, String authType) { }
                public void checkServerTrusted(X509Certificate[] certs, String authType) { }

            } };

            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            // Create all-trusting host name verifier
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) { return true; }
            };
            // Install the all-trusting host verifier
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
            /* End of the fix*/



            url = new URL(fileUrl);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            String imageName =  filePath+saveFileName;

            FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            byte[] context=output.toByteArray();
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
            return saveFileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //链接url下载图片
    public static String downloadPicture1(String fileUrl,String filePath,String name) {
        URL url = null;
        int imageNumber = 0;
        int last=fileUrl.lastIndexOf(".");
        String suffix="";
        if(last>-1){
            suffix=fileUrl.substring(last);
        }
        Random r=new Random();
        String saveFileName= name+"-"+r.nextInt(100)+suffix;
        try {

            //覆盖我的项目中的SSL检查
            /* Start of Fix */
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; }
                public void checkClientTrusted(X509Certificate[] certs, String authType) { }
                public void checkServerTrusted(X509Certificate[] certs, String authType) { }

            } };

            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            // Create all-trusting host name verifier
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) { return true; }
            };
            // Install the all-trusting host verifier
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
            /* End of the fix*/



            url = new URL(fileUrl);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            String imageName =  filePath+saveFileName;

            FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            byte[] context=output.toByteArray();
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
            return saveFileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
